package com.clouddemo.controller;

import com.clouddemo.entities.TabUsers;
import com.clouddemo.service.TabUsersService;
import com.clouddemo.util.CreateUUID;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class TabUsersController {

    @Resource
    private TabUsersService service;


    @GetMapping(value = "/tab_users_findall")
    public String tab_users_findall(){
        JSONArray json = new JSONArray();
        List<TabUsers> list = service.findall();
        for (TabUsers t:list){
            JSONObject jo = new JSONObject();
            String id = t.getId();
            String loginname = t.getLoginname();
            String username = t.getUsername();
            String password = t.getPassword();
            jo.put("id",id);
            jo.put("loginname",loginname);
            jo.put("username",username);
            jo.put("password",password);
            json.add(jo);
        }
        return json.toJSONString();
    }

    @PostMapping(value = "/tab_users_save")
    public String save(@RequestBody TabUsers u){
        String id = CreateUUID.getuuid();
        u.setId(id);
        JSONObject jo = new JSONObject();
        boolean b = service.save(u);
        if(b) {
            jo.put("code", "200");
            jo.put("msg", "success");
        }else{
            jo.put("code", "500");
            jo.put("msg", "error");
        }
        return jo.toJSONString();
    }

}
